package edu.pitt.cs3530.scripts;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class InsertCount {
	public static void main(String args[]) {
		File fileArg = new File(args[0]);
		long interval = Integer.parseInt(args[1]);
		List<File> fileList = new ArrayList<File>();
		if (fileArg.isDirectory()) {
			for (File f : fileArg.listFiles()) {
				if (!f.isDirectory()) {
					fileList.add(f);
				}
			}
		} else {
			fileList.add(fileArg);
		}
		for (File file : fileList) {
			try {
				int i = 0;
				BufferedReader reader = new BufferedReader(new FileReader(file));
				List<String> lines = new LinkedList<String>();
				while (reader.ready()) {
					lines.add(reader.readLine());
				}
				BufferedWriter writer = new BufferedWriter(new FileWriter(file));
				for (String line : lines) {
					String toWrite = i + ":" + line + "\r\n";
					writer.write(toWrite);
					i += interval;
				}
				writer.close();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
